/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unc.pds.auth;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import unc.pds.exc.CannotInitSecurityException;
import unc.pds.exc.PDSSecurityException;
import unc.pds.model.UserRemote;
import unc.pds.service.LoginRemote;
import unc.pds.service.LoginRemoteHome;
import unc.pds.service.ServiceBeanRemote;

/**
 *
 * @author AIN
 */
public class SecurityST {

    public static int SECURITY_ACCESS = 1;
    public static String SECURITY_ACCESS_ST = "access";
    public static int SECURITY_NIETHER = 0;
    public static int SECURITY_DENIED = -1;
    public static String SECURITY_DENIED_ST = "denied";

    public static int SECURITY_DEFAULT = 0;
    public static boolean SECURITY_DEFAULT_RES = false;

    public static String SECURITY_DEFAULT_ROLE = "default";
    public static String SECURITY_ANONIMOUS_ROLE = "anonimous";

   /* static UserRemote user;
    public static void login(String nick, String pass) throws RemoteException{
        try {
            LoginRemote LoginR = ((LoginRemoteHome) (new InitialContext()).lookup("ejb/Login")).create();
            ServiceBeanRemote sbr = LoginR.login("ain", "1234");
            user =  sbr.getUser();
        } catch (Exception ex) {
            throw new RemoteException("login: "+ex.getLocalizedMessage());
        }
    }
    public static UserRemote getCurrentUser() throws  RemoteException {
        return user;
    }

    public static boolean isAnonimous(){
        if (user == null) return true; else return false;
    }
*/
    public static Long LoginSessionKey = new Long(0);
    public static IPDSSecurity getSecurity(Long sessionKey) throws CreateException, RemoteException, NamingException, FinderException{
        PSDSecurityRemoteHome sbrh = (PSDSecurityRemoteHome) (new InitialContext()).lookup("ejb/PSDSecurity");
        if (sessionKey==null) sessionKey = LoginSessionKey;
        return sbrh.create(sessionKey);
        //return new SecStupidImpl(sessionKey);
    }

    public static ISecurityApplyer getSecurityApplyer(Long sessionKey) throws PDSSecurityException{
        try {
            SecurityApplyerRemoteHome sbrh = (SecurityApplyerRemoteHome) (new InitialContext()).lookup("ejb/SecurityApplyer");
            if (sessionKey == null) {
                sessionKey = LoginSessionKey;
            }
            return sbrh.create();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CannotInitSecurityException(ex.getLocalizedMessage());
        }
    }
}
